package no.gnet.edvd.cmdline;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.sql.DataSource;

import no.gnet.edvd.database.mysql.MySQLDatastore;
import no.gnet.edvd.types.Player;
import no.gnet.edvd.util.Utilities;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;

public class PlayerAdmin {
	
	ApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");

	
	protected DataSource datasource = null;
	
	MySQLDatastore mysqlds = null;
	
	public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException{
		PlayerAdmin admin = new PlayerAdmin();
		admin.addPlayer(args[0], args[1], args[2], args[3]);
	}
	
	public PlayerAdmin() throws UnsupportedEncodingException, NoSuchAlgorithmException{
		datasource = (DataSource) context.getBean("dataSource");
		mysqlds = new MySQLDatastore(datasource);
	}
	
	public void addPlayer(String email, String firstname, String lastname, String pw) throws UnsupportedEncodingException, NoSuchAlgorithmException{
		Assert.notNull(email);
		Assert.notNull(firstname);
		Assert.notNull(lastname);
		Assert.notNull(pw);
		Player player = new Player(firstname, lastname, email);
		mysqlds.storePlayerWithPassword(player, Utilities.hashAndHexEncodePw(pw));
		Player storedp = mysqlds.getPlayer(player);
		System.out.println("Stored: " + storedp);
	}
	

}
